package com.hsingh.eclipse.mercury.extension.command;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.Command;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.commands.ICommandService;

import com.hsingh.eclipse.mercury.extensionpoint.definition.ITask;
import com.hsingh.eclipse.mercury.extensionpoint.definition.ITaskProvider;

public class CommandTaskProvider implements ITaskProvider {

  public CommandTaskProvider() {}

  @Override
  public List<ITask> getTasks(IEvaluationContext context) {
    IWorkbenchSite parentSite = (IWorkbenchSite) context.getVariable("activeSite");
    ICommandService cmdService = (ICommandService) parentSite.getService(ICommandService.class);

    Command[] cmds = cmdService.getDefinedCommands();

    ArrayList<ITask> list = new ArrayList<ITask>();
    for (Command cmd : cmds) {
      if (cmd.isEnabled() && cmd.isHandled()) {
        list.add(new CommandTask(cmd));
      }
    }

    return list;
  }

}
